package main

import (
	"fmt"
	"net/http"
	"sync"
)

var productNum int64 = 10000

// 当前访问总数
var sum int64 = 0

// 互斥锁
var mutex sync.Mutex

func getOne() bool {
	// 加锁
	mutex.Lock()
	defer mutex.Unlock()
	if sum < productNum {
		fmt.Println("当前数量",sum)
		sum++
		return true
	}
	return false
}

func getProduct(w http.ResponseWriter, r *http.Request) {
	if getOne() {
		w.Write([]byte("true"))
		return
	}
	w.Write([]byte("false"))
	return
}


func main() {

	http.HandleFunc("/getOne",getProduct)
	//http.Handle("/getOne",http.HandlerFunc(getProduct))
	http.ListenAndServe(":8083",nil)


}
